package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Component
@Slf4j
public class Template20 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("20KEY-2")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("20KEY-1")){
                        textRun.setText(data.get("dataPeriod").asText());
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text异常原因:{}",20,e);
                }
            }
        }
    }

//    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
//        List<SeriesData> barData = getBarData(data);
//        templateUtil.barChart(plot,barData,workbook,sheet,chart);
//    }
//
//    private List<SeriesData> getBarData(JsonNode jsonNode){
//        JsonNode jsonNode1 = jsonNode.get("utcClubs");
//
//        List<SeriesData> seriesDatas = new ArrayList<>();
//        List<NameDouble> utc = new ArrayList<>();
//        List<NameDouble> gClub = new ArrayList<>();
//
//        for (JsonNode jsonNode2:jsonNode1) {
//            String date = jsonNode2.get("date").asText();
//            NameDouble utcNameDouble = new NameDouble(date, StringUtil.doubleFormat(jsonNode2.get("utc").asText()));
//            NameDouble gClubNameDouble = new NameDouble(date,StringUtil.doubleFormat(jsonNode2.get("club").asText()));
//            utc.add(utcNameDouble);
//            gClub.add(gClubNameDouble);
//        }
//        SeriesData tempSeriesData1 = new SeriesData("UTC授权人数",utc,"");
//        SeriesData tempSeriesData2 = new SeriesData("成为G Club新用户",gClub,"");
//        seriesDatas.add(tempSeriesData1);
//        seriesDatas.add(tempSeriesData2);
//        return seriesDatas;
//    }
//
//    public void lineChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
//        List<SeriesData> lineData = getLineData(data);
//        templateUtil.lineChart(plot,lineData,workbook,sheet,chart,2);
//    }
//
//    private List<SeriesData> getLineData(JsonNode jsonNode){
//        JsonNode jsonNode1 = jsonNode.get("utcClubs");
//        List<SeriesData> seriesDatas = new ArrayList<>();
//        List<NameDouble> utc = new ArrayList<>();
//
//        for (JsonNode jsonNode2:jsonNode1) {
//            NameDouble utcNameDouble = new NameDouble(jsonNode2.get("date").asText(),StringUtil.doubleFormat2(jsonNode2.get("rate").asText())/100);
//            utc.add(utcNameDouble);
//        }
//        SeriesData tempSeriesData1 = new SeriesData("UTC跳转成为G Club新会员比率",utc,"");
//        seriesDatas.add(tempSeriesData1);
//        return seriesDatas;
//    }

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape) shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();
        //遍历获取每一个单元格
        for (int row = 0; row < numberOfRows; row++) {
            for (int col = 1; col < numberOfColumns; col++) {
                TableCell cell = tableShape.getCell(row, col);
                if (row == 0 && col == 4){
                    XSLFTextParagraph cellParagraph = (XSLFTextParagraph) cell.getTextParagraphs().get(0);
                    XSLFTextRun textRun = cellParagraph.getTextRuns().get(0);
                    textRun.setText(DateUtil.getLastSunday().getMonthValue()+"月预计拉新KPI");
                    continue;
                }
                if (row == 1) {
                    XSLFTextParagraph cellParagraph = (XSLFTextParagraph) cell.getTextParagraphs().get(0);
                    XSLFTextRun textRun = cellParagraph.getTextRuns().get(0);
                    if (col == 3) {
                        textRun.setText(data.get("monthKpi").get("actualKpi").asText());
                        continue;
                    }
                    if (col == 4) {
                        textRun.setText(data.get("monthKpi").get("predictKpi").asText());
                        continue;
                    }
                    if (col == 5) {
                        textRun.setText(data.get("monthKpi").get("rate").asText());
                        continue;
                    }
//                    if (col == 6) {
//                        textRun.setText(data.get("monthKpi").get("remark").asText());
//                    }
                    continue;
                }
                if (row == 3) {
                    XSLFTextParagraph cellParagraph = (XSLFTextParagraph) cell.getTextParagraphs().get(0);
                    XSLFTextRun textRun = cellParagraph.getTextRuns().get(0);
                    if (col == 3) {
                        textRun.setText(data.get("yearKpi").get("actualKpi").asText());
                        continue;
                    }
                    if (col == 4) {
                        textRun.setText(data.get("yearKpi").get("predictKpi").asText());
                        continue;
                    }
                    if (col == 5) {
                        textRun.setText(data.get("yearKpi").get("rate").asText());
//                        continue;
                    }
//                    if (col == 6) {
//                        textRun.setText(data.get("yearKpi").get("remark").asText());
//                    }
                }
            }
        }
    }
}
